<?php
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
require_once($GLOBALS['BIB']."Objects/obj.mssql.php");	
$row = 0;
$field = '';
$numb = '';
$fi = '';
$lcount = split('_',$_FILES['file']['name']);
$logfile = 'BVA'.substr($_FILES['file']['name'],0,strlen($_FILES['file']['name'])-3).'txt';
$handle = fopen($_FILES['file']["tmp_name"], "r");
$sum = 0;
$tmp_array = array("811136959",
"811136960",
"811136964",
"812122143",
"812122145",
"812122157",
"812122160",
"812122161",
"812122166",
"812122170",
"813129441",
"813129444",
"813129449",
"813129452",
"813129454",
"813129457",
"813129458",
"813129461",
"814194065",
"814194078",
"814194099",
"814194125",
"814194146",
"814194150",
"814194156",
"814194186",
"815128704",
"815128705",
"815128713",
"815128717",
"815128718",
"815128723",
"815128726",
"817137189",
"817137191",
"817137200",
"817137201",
"823022791",
"823022794",
"823022799",
"823022806",
"823022809",
"823022810",
"823022812",
"823022813",
"823022814",
"823022816",
"823022817",
"823022819",
"823022824",
"823022825",
"823022830",
"825005982",
"825005988",
"826014331",
"826014332",
"826014334",
"826014336",
"826014342",
"826014344",
"826014346",
"826014348",
"826014352",
"826014355",
"826014356",
"826014358",
"827002199",
"827002200",
"827002202",
"827002204",
"827002205",
"827002209",
"827002210",
"827002211",
"827002212");

while (($data = fgetcsv($handle, 3000, ",")) !== FALSE) {
	if (in_array(str_replace('-','',$data[0]),$tmp_array)) {
    $row++;
	$numb[$row] = $data[0].',';
	$field[$row] = $data[1].','.date("dmY").','.$data[2].',';
	$Ag = preg_replace( "#[^0-9\-\x\X]#", "",$data[5]);
	$Ag = split('-',$Ag);
	$LAST = count($Ag)-1;
	$fieldv[$row] = str_pad($Ag[0], 6, "0", STR_PAD_LEFT).',';
	if ($LAST > 0) {
	$fieldv[$row] .= $Ag[$LAST];
	}
	$fieldv[$row] .= ','.str_pad(preg_replace( "#[^0-9\x\X]#", "",$data[6]), 15, "0", STR_PAD_LEFT).',';
	$fieldv[$row] .= str_pad($data[3], 14, "0", STR_PAD_LEFT).','.str_pad($data[7], 16, "0", STR_PAD_LEFT).'00,';
	$fieldv[$row] .= $data[9];
	$sum = $sum+$data[7];
	$cb = $row;
	}
	}

fclose($handle);
$Id = '';
	for ($a = 1; $a <= $cb; $a++) {
		$Id .= $numb[$a];
	}
	$ccj = strlen($Id);
	$Id = substr($Id,0,strlen($Id)-1);

	$dir = 'tmp_files/';
	$saveLocation=$dir . $logfile;
	$handle = fopen($saveLocation,"x");

	LigarMSBSD($BSD['DB01']);
		
		$Names = $GLOBALS['MSCONN']->db_array("SELECT    
			 bpp_IdBanco
		FROM         
			Facturas
		INNER JOIN beneficiarioscuentas ON FraCtaBenef = cb_Id
		INNER JOIN hd_bancospagadores ON bpp_NombreBanco = cb_Banco AND bpp_Corr = 300
		WHERE FraCorresponsal = 300
		AND FraReferenciaCo IN ({$Id}) ORDER BY FraReferenciaCo");
	
		for ($b = 1; $b <= $Names[0]; $b++) {
		$fi .= $field[$b].str_pad($Names[$b]['bpp_IdBanco'], 4, "0", STR_PAD_LEFT).','.$fieldv[$b].PHP_EOL;
		
		}
		$he = date("Y").str_pad(($lcount[1]-157), 9, "0", STR_PAD_LEFT).','.$lcount[2].','.str_pad($b-1, 5, "0", STR_PAD_LEFT).','.str_pad($sum, 16, "0", STR_PAD_LEFT).'00'.PHP_EOL;
	fwrite($handle,$he);
	fwrite($handle,$fi);
	fclose($handle);
	echo '<a href="'.$saveLocation.'">'.$logfile.'</a>';
?>